//
//  LampMessageViewController.m
//  YIJU2.0-HD
//
//  Created by 陈柏兵 on 16/5/10.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "LampMessageViewController.h"
#import "Macro.h"
#import "Masonry.h"
#import "UIButton+NavRightBtn.h"
#import "SearchView.h"
#import "ModelLampColorSetUpView.h"

@interface LampMessageViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *sureBtn;

@property (nonatomic, strong) NSArray *titleArray;

@property (nonatomic, strong) ModelLampColorSetUpView *modelLampColor;

@end

@implementation LampMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kSettingBackgroundColor;
    [self titleArray];
    [self LayOutUI];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSInteger textTag = textField.tag - 100;
    DDLogInfo(@"点击了第-%ld-个TextField",textTag);
}

#pragma mark - privte method
- (void)LayOutUI {
    [self sureBtn];
    
    SearchView *lastSearch = nil;
    for (int i = 0; i<4; i ++) {
        SearchView *editView = [[SearchView alloc] initWithTitle:_titleArray[i] titleFont:kTitle_Font cornerRadius:3 backgroundColor:[UIColor whiteColor]];
        [self.view addSubview:editView];
        for (UITextField *MyView in editView.subviews) {
            if ([MyView isKindOfClass:[UITextField class]]) {
                MyView.tag = 100 +i;
                MyView.delegate = self;
            }
        }
        
        if (lastSearch == nil) {
            [editView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.view.mas_top).offset(84);
                make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
                make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
                make.height.equalTo(self.view.mas_height).multipliedBy(1/16.0f);
            }];
        } else {
            [editView mas_makeConstraints:^(MASConstraintMaker *make) {
                [editView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lastSearch.mas_bottom).offset(TweentyOffsetViewMargin);
                    make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
                    make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
                    make.height.equalTo(self.view.mas_height).multipliedBy(1/16.0f);
                }];
            }];
        }
        lastSearch = editView;
        
        if (i==3) {
            [self.modelLampColor mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(editView.mas_bottom).offset(TweentyOffsetViewMargin);
                make.left.equalTo(self.view.mas_left).offset(TweentyOffsetViewMargin);
                make.right.equalTo(self.view.mas_right).offset(-TweentyOffsetViewMargin);
                make.height.equalTo(self.view.mas_height).multipliedBy(1/16.0f);
            }];
        }

    }
    
    
}

#pragma mark - Event Response
- (void)handleBtnAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Getter method
- (UIButton *)sureBtn {
    if (_sureBtn == nil) {
        _sureBtn = [UIButton btnWithTitle:@"确定"];
        _sureBtn.tag = 400;
        [_sureBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithCustomView:_sureBtn];
        self.navigationItem.rightBarButtonItem = editItem;
    }
    return _sureBtn;
}

- (NSArray *)titleArray {
    if (_titleArray == nil) {
        _titleArray = @[@"名称:",@"IP:",@"Port:",@"Num:"];
    }
    return _titleArray;
}

- (ModelLampColorSetUpView *)modelLampColor {
    if (_modelLampColor == nil) {
        _modelLampColor = [[ModelLampColorSetUpView alloc] initWithLampColor:^(ModelLampColorSetUpType ModelLampColorSetUpType) {
            DDLogInfo(@"ModelLampColorSetUpType = %ld",ModelLampColorSetUpType);
        }];
        [self.view addSubview:_modelLampColor];
    }
    return _modelLampColor;
}

@end
